//
//  ZJFirstRecMoreStoryViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/24.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJFirstRecMoreStoryViewController.h"
#import "ZJRecFirstStoryCollectionViewCell.h"
#import "StoryDetailViewController.h"

@interface ZJFirstRecMoreStoryViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *tableArray;
@property(nonatomic,retain)UICollectionView *collectionView;

@end

@implementation ZJFirstRecMoreStoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, 15, 50, 20)];
    titleLabel.text = @"更多故事";
    titleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH/2-23, 200);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 20;
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //设置item与边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 20, 15);
    
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.92 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ZJRecFirstStoryCollectionViewCell class] forCellWithReuseIdentifier:@"ZJStoryCollectionViewCell"];
    
#pragma mark 初始化
    
    self.tableArray = [NSMutableArray array];
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";

    [AFNetworkTool getUrl:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0" body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableDictionary *dic = result[@"data"];
        NSMutableArray *array = [dic objectForKey:@"hot_spot_list"];
        for (NSMutableDictionary *dic in array) {
            ZJTableModel *tableModel = [ZJTableModel modelWithDictionary:dic];
            [self.tableArray addObject:tableModel];
        }
        [self.collectionView reloadData];
        
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

//多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.tableArray.count;
}

//每行内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"ZJStoryCollectionViewCell";
    ZJRecFirstStoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    ZJTableModel *tableModel = [self.tableArray objectAtIndex:indexPath.row];
    cell.tableModel = tableModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    StoryDetailViewController *reStoryDVC = [StoryDetailViewController new];
    [self.navigationController pushViewController:reStoryDVC animated:NO];
    reStoryDVC.urlStr = [[self.tableArray objectAtIndex:indexPath.row] spot_id];
    
}

//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
